package com.xxxy.jsq.action.record;

import com.xxxy.jsq.dao.RecordDao;
import com.xxxy.jsq.model.Record;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet("/updateRecordServlet")
public class updateRecordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        Record model = new Record();
        RecordDao dao = new RecordDao();

        // 获取参数
        model.setRecordId(Long.parseLong(request.getParameter("recordId")));
        model.setRecordName(request.getParameter("recordName"));
        model.setRecordMemo(request.getParameter("recordMemo"));
        model.setRecordFlag(request.getParameter("recordFlag"));

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            model.setRecordDate(sdf.parse(request.getParameter("recordDate")));
        } catch (ParseException e) {
            response.getWriter().print("日期格式错误");
            return;
        }

        String result = dao.update(model);
        PrintWriter out = response.getWriter();
        out.print(result);
        out.flush();
        out.close(); 
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }
}